﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;
using System.IO;
using System.Collections.Specialized;
using ThietBiSo.Helper;
using ThietBiSo.UrlReview;


namespace ThietBiSo.Controllers
{
    public class sanphamController : ApplicationController
    {
        //
        // GET: /sanpham/

        private MSanPham msp = new MSanPham();
        private SanPham s = new SanPham();
        SEOUrl seo = new SEOUrl();

        //private MDanhMucCon mdmc = new MDanhMucCon();
        //private DanhMucCon dmc = new DanhMucCon();

        MDanhMuc mdm = new MDanhMuc();
        DanhMuc tbldm = new DanhMuc();

        #region admin

        public ActionResult Index(int? id, string cbNhom)
        {
            string nhom = cbNhom == null ? "" : cbNhom;
            int pageString = id ?? 0;
            int page = pageString == 0 ? 1 : pageString;

            string param = "?cbNhom=" + nhom;

            ViewData["cbNhom"] = cbNhom;
            int total;
            if (cbNhom == null || cbNhom == "")
            {
                total = msp.GetTotal();
            }
            else
            {

                total = msp.GetTotal(cbNhom);
            }

            PagingController("/SanPham/Index/", total, page, param);

          
            tbldm.MaDanhMuc = 0;
            tbldm.TenDanhMuc = "[Tất cả]";
            List<DanhMuc> group = mdm.FindAll();
            group.Insert(0, tbldm);
            this.ViewData["group"] = new SelectList(group, "MaDanhMuc", "TenDanhMuc", nhom);


            List<SanPham> sps = new List<SanPham>();
            if (cbNhom == "" || cbNhom == null || cbNhom == "0")
            {
                sps = msp.GetList(start, offset);
            }
            else
            {
                sps = msp.GetList(start, offset, nhom);
            }
            ViewData["sanphams"] = sps;
            return View();
        }

        public ActionResult Create()
        {
            string urlcbNhom = Request.QueryString["cbNhom"];
            ViewData["urlcbNhom"] = urlcbNhom;
          
            return View();
        }

        //
        // POST: /SanPham/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            string urlcbNhom = collection.Get("cbNhom");

            try
            {               
                s.MaSP = msp.GetMax() + 1;
                s.TenSP = collection.Get("TenSP");

                s.HinhAnh = collection.Get("HinhAnh");

                s.ChiTiet = System.Net.WebUtility.HtmlDecode(collection.Get("ChiTiet"));


                s.MaDM = int.Parse(urlcbNhom);


                s.TrangChu = true;
                s.HienThi = true;
                if (msp.GetMaxTT() == 0 || msp.GetTotal() == 0)
                {
                    s.ThuTu = 1;
                }
                else
                    s.ThuTu = msp.GetMaxTT() + 1;
                msp.Save(s);


                return Redirect("/sanpham?cbNhom=" + urlcbNhom);
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            ViewData["sanphams"] = msp.GetByID(id);
            string urlcbNhom = Request.QueryString["cbNhom"];
            ViewData["urlcbNhom"] = urlcbNhom;

            return View();
        }

        //
        // POST: /LinhKien/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                string urlcbNhom = collection.Get("cbNhom");
                s.MaSP = id;
                if (collection.Get("HinhAnh") == null || collection.Get("HinhAnh") == "")
                {
                    s.HinhAnh = collection.Get("HiddenHinhAnh");
                }
                else
                {
                    s.HinhAnh = collection.Get("HinhAnh");
                }
                s.MaDM = int.Parse(collection.Get("ddlDanhMuc"));
                s.TenSP = collection.Get("TenSP");
                s.ChiTiet = System.Net.WebUtility.HtmlDecode(collection.Get("ChiTiet"));
                s.ThuTu = int.Parse(collection.Get("ThuTu"));
                msp.Update(s);
                return Redirect("/sanpham?cbNhom=" + urlcbNhom);
            }
            catch
            {
                return View();
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int ma)
        {

            string error = msp.Delete(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult HienThi(int ma)
        {

            string error = msp.HienThi(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TrangChu(int ma)
        {

            string error = msp.TrangChu(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }
        #endregion

        #region website
        public ActionResult chitiet(string id)
        {

            ViewData["chitiets"] = msp.ChiTiet(int.Parse(seo.CatChuoi(id)));
            ViewData["title"] = msp.GetTitleByMa(int.Parse(seo.CatChuoi(id)));
           
            return View();
        }
        #endregion


    }
}
